/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cenluan.common;

import com.cenluan.IndexController;
import com.cenluan.activity.Activity;
import com.cenluan.activity.ActivityController;
import com.cenluan.activity.comment.ActivityComment;
import com.cenluan.activity.comment.ActivityCommentController;
import com.cenluan.activity.member.ActivityMember;
import com.cenluan.activity.member.ActivityMemberController;
import com.cenluan.blog.Blog;
import com.cenluan.blog.BlogController;
import com.cenluan.blog.comment.BlogComment;
import com.cenluan.blog.comment.BlogCommentController;
import com.cenluan.blog.favorite.BlogFavoriteController;
import com.cenluan.ext.beetl.format.PrettyTimeFormat;
import com.cenluan.ext.beetl.format.UrlFormat;
import com.cenluan.ext.beetl.format.XSSDefenseFormat;
import com.cenluan.gallery.Gallery;
import com.cenluan.gallery.GalleryController;
import com.cenluan.gallery.member.GalleryMember;
import com.cenluan.gallery.member.GalleryMemberController;
import com.cenluan.login.LoginController;
import com.cenluan.login.LoginInterceptor;
import com.cenluan.markerdown.MarkdownController;
import com.cenluan.me.MeController;
import com.cenluan.me.call.Call;
import com.cenluan.me.call.MyCallController;
import com.cenluan.me.data.MydataController;
import com.cenluan.me.log.MyLogController;
import com.cenluan.me.message.Message;
import com.cenluan.me.message.MyMessageController;
import com.cenluan.me.portrait.PortraitController;
import com.cenluan.mi.MiController;
import com.cenluan.photo.Photo;
import com.cenluan.photo.PhotoController;
import com.cenluan.photo.favorite.FavoriteController;
import com.cenluan.qq.QQController;
import com.cenluan.session.SessionInViewInterceptor;
import com.cenluan.tweet.Tweet;
import com.cenluan.tweet.TweetController;
import com.cenluan.tweet.comment.TweetComment;
import com.cenluan.tweet.comment.TweetCommentController;
import com.cenluan.tweet.photo.TweetPhoto;
import com.cenluan.tweet.photo.TweetPhotoController;
import com.cenluan.user.User;
import com.cenluan.user.UserController;
import com.cenluan.user.log.UserLog;
import com.cenluan.vote.Vote;
import com.cenluan.vote.VoteController;
import com.cenluan.vote.VoteOption;
import com.jfinal.config.*;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import org.beetl.core.GroupTemplate;
import org.beetl.ext.jfinal.BeetlRenderFactory;

import java.io.File;
import java.util.HashMap;

public class Config extends JFinalConfig {

    @Override
    public void configConstant(Constants me) {
        loadPropertyFile("env.properties");
        // 开发模式
        if (getPropertyToBoolean("devMode")) {
            me.setDevMode(true);
        }
        // 错误页面使用jfinal默认的，不再设置
        // beetl
        me.setMainRenderFactory(new BeetlRenderFactory(PathKit.getWebRootPath()
                + "/WEB-INF/beetl"));
        GroupTemplate groupTemplate = BeetlRenderFactory.groupTemplate;
        groupTemplate.registerFormat("xss", new XSSDefenseFormat());
        groupTemplate.registerFormat("prettyTime", new PrettyTimeFormat());
        groupTemplate.registerFormat("url", new UrlFormat());
        if (!getPropertyToBoolean("devMode")) {
            groupTemplate.getConf().getResourceMap().put("autoCheck", "false");
        }
        // 更改tag文件后缀
        groupTemplate.getConf().getResourceMap().put("tagSuffix", "html");
        groupTemplate.getConf().setStatementStart("<!--@");
        groupTemplate.getConf().setStatementEnd("-->");
        // 设置
        groupTemplate.setSharedVars(new HashMap<String, Object>());
        groupTemplate.getSharedVars().put("devMode",
                getPropertyToBoolean("devMode"));

        // 设置下载文件大小和存放位置
        me.setUploadedFileSaveDirectory(PathKit.getWebRootPath()
                + File.separator + "upload");
    }

    @Override
    public void configRoute(Routes me) {
        me.add("/", IndexController.class);
        me.add("/login", LoginController.class);
        me.add("/photo", PhotoController.class);
        me.add("/photo/favorite", FavoriteController.class);
        me.add("/gallery", GalleryController.class);
        me.add("/gallery/member", GalleryMemberController.class);
        me.add("/user", UserController.class, "/user");
        me.add("/mi", MiController.class);
        me.add("/qq", QQController.class);
        // 投票
        me.add("/vote", VoteController.class);
        // markerdown
        me.add("/markdown", MarkdownController.class);
        // 微博
        me.add("/tweet", TweetController.class);
        me.add("/tweet/comment", TweetCommentController.class);
        me.add("/tweet/photo", TweetPhotoController.class);
        // 博客
        me.add("/blog", BlogController.class);
        me.add("/blog/comment", BlogCommentController.class);
        me.add("/blog/favorite", BlogFavoriteController.class);
        // 活动
        me.add("/activity", ActivityController.class);
        me.add("/activity/comment", ActivityCommentController.class);
        me.add("/activity/member", ActivityMemberController.class);
        // 个人中心
        me.add("/me", MeController.class);
        me.add("/me/call", MyCallController.class);
        me.add("/me/message", MyMessageController.class);
        me.add("/me/log", MyLogController.class);
        me.add("/me/data", MydataController.class);
        me.add("/me/portrait", PortraitController.class);
    }

    @Override
    public void configPlugin(Plugins me) {
        // ehcache
        me.add(new EhCachePlugin(Thread.currentThread().getContextClassLoader()
                .getResource("ehcache.xml")));
        // druid
        DruidPlugin druidPlugin = new DruidPlugin(getProperty("mysql.url"),
                getProperty("mysql.username"), getProperty("mysql.password"),
                getProperty("mysql.driverClass"));
        druidPlugin
                .setMaxPoolPreparedStatementPerConnectionSize(getPropertyToInt("druid.maxPoolPreparedStatementPerConnectionSize"));
        me.add(druidPlugin);
        druidPlugin.setInitialSize(getPropertyToInt("druid.initialSize"));
        druidPlugin.setMinIdle(getPropertyToInt("druid.minIdle"));
        druidPlugin.setMaxActive(getPropertyToInt("druid.maxActive"));
        ActiveRecordPlugin mysqlArp = new ActiveRecordPlugin(druidPlugin);
        mysqlArp.setDialect(new MysqlDialect());
        if (getPropertyToBoolean("showSql")) {
            mysqlArp.setShowSql(true);
        }
        mysqlArp.addMapping("t_user", User.class);
        mysqlArp.addMapping("t_call", Call.class);
        mysqlArp.addMapping("t_gallery", Gallery.class);
        mysqlArp.addMapping("t_photo", Photo.class);
        mysqlArp.addMapping("t_profiles", Profiles.class);
        mysqlArp.addMapping("t_gallery_member", GalleryMember.class);
        mysqlArp.addMapping("t_message", Message.class);
        mysqlArp.addMapping("t_user_log", UserLog.class);
        mysqlArp.addMapping("t_vote", Vote.class);
        mysqlArp.addMapping("t_vote_option", VoteOption.class);
        mysqlArp.addMapping("t_tweet", Tweet.class);
        mysqlArp.addMapping("t_tweet_photo", TweetPhoto.class);
        mysqlArp.addMapping("t_tweet_comment", TweetComment.class);
        // 博客
        mysqlArp.addMapping("t_blog", Blog.class);
        mysqlArp.addMapping("t_blog_comment", BlogComment.class);
        // 活动
        mysqlArp.addMapping("t_activity", Activity.class);
        mysqlArp.addMapping("t_activity_comment", ActivityComment.class);
        mysqlArp.addMapping("t_activity_member", ActivityMember.class);
        me.add(mysqlArp);

    }

    @Override
    public void configInterceptor(Interceptors me) {
        me.add(new LoginInterceptor());
        me.add(new SessionInViewInterceptor());

    }

    @Override
    public void configHandler(Handlers me) {

    }

}
